<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2021/1/7
 * Time: 9:42
 */
namespace App\Service\Api;

use App\Http\Model\Mysql\ProductModel;
use App\Http\Model\Mysql\UserIntegralModel;
use App\Http\Model\Mysql\OrderIntegralModel;
use App\Http\Model\Mysql\ProductIntegralModel;

class IntegralService extends BaseService
{
    /**
     * 积分商品列表
     * @param int $is_vip
     * @param int $page
     * @return array
     */
    public function integralGoods(int $is_vip = 1, int $page = 1)
    {
        $count      =   ProductIntegralModel::where(['is_vip' => $is_vip, 'is_shelf' => ProductIntegralModel::IS_SHELF])->count();
        $good_list  =   ProductIntegralModel::with([
                            'good' => function ($query) {
                                $query->sell()->select(['id', 'unique_id', 'name', 'sku_code', 'sell_price', 'original_price', 'cost_price', 'cover_pic', 'cover_video']);
                            },
                            'spec' =>  function ($query) {
                                $query->select(['id', 'product_id', 'attr_list', 'sell_price', 'spec_pic', 'sku_code', 'stock']);
                            },
                        ])->where(['is_vip' => $is_vip, 'is_shelf' => ProductIntegralModel::IS_SHELF])->offset(($page - 1) * self::PAGE_LIMIT)->limit(self::PAGE_LIMIT)
                        ->select(['id', 'good_id', 'spec_id', 'is_vip', 'is_shelf', 'integral'])->get()->each(function (&$item) {
                            if ($item->spec->attr_list) {
                                $item->spec->attr_value =   json_decode($item->spec->attr_list, true);
                            }
                        });

        if ($good_list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'current'=> $page,
                'pages' =>  ceil($count / self::PAGE_LIMIT),
                'list'  =>  $good_list
            ]];
        }
    }

    /**
     * 查看用户积分
     * @return array
     */
    public function integralPersonal()
    {
        $user_info  =   auth('api')->user();
        if (empty($user_info)) {
            return [false, '用户信息错误'];
        } else {
            return [true, [
                'user_id'   =>  $user_info->uid,
                'integral'  =>  $user_info->integral
            ]];
        }
    }

    /**
     * 用户兑换记录
     * @param int $page
     * @return array
     */
    public function integralExchange(int $page = 1)
    {
        $user_info  =   request()->post('api_user');
        $user_id    =   $user_info['user_id'];

        $count      =   OrderIntegralModel::where(['user_id' => $user_id, 'is_pay' => 1])->count();
        $order_list =   OrderIntegralModel::with([
                            'good'  =>  function ($query) {
                                $query->select(['id', 'name', 'cover_pic']);
                            }
                        ])->where(['user_id' => $user_id, 'is_pay' => 1])->offset(($page - 1) * self::PAGE_LIMIT)->limit(self::PAGE_LIMIT)
                        ->select(['id', 'user_id', 'good_id', 'spec_id', 'created_at', 'is_pay', 'total_integral'])->get()->each(function (&$item){
                            $item->create_time  =   $item->created_at->toDateString();
                            unset($item->created_at);
                        });

        if ($order_list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'current'=> $page,
                'pages' =>  ceil($count / self::PAGE_LIMIT),
                'list'  =>  $order_list
            ]];
        }
    }

    /**
     * 用户积分收入支付记录
     * @param int $page
     * @return array
     */
    public function integralIncome(int $page = 1)
    {
        $user_info  =   request()->post('api_user');
        $user_id    =   $user_info['user_id'];

        $count      =   UserIntegralModel::where(['user_id' => $user_id])->count();
        $income_list=   UserIntegralModel::where(['user_id' => $user_id])->offset(($page - 1) * self::PAGE_LIMIT)->limit(self::PAGE_LIMIT)
                        ->select(['id', 'user_id', 'integral', 'created_at', 'type', 'reason', 'order_id'])->get()->each(function (&$item){
                            if ($item->order_id != 0) {
                                $good_info  =   ProductModel::where(['id' => OrderIntegralModel::where(['id' => $item->order_id])->value('good_id') ?? 0])->get(['id', 'name', 'cover_pic'])->first();
                                $item->desc =   !empty($good_info) ? $good_info->name : '';
                                $item->pic  =   !empty($good_info) ? $good_info->cover_pic : '';
                            } else {
                                $item->desc =   '';
                                $item->pic  =   '';
                            }

                            if ($item->type == 2 && $item->reason == 2) {
                                $item->desc =   '网虹城VIP购买';
                                $item->pic  =   '';
                            }
                            $item->create_time  =   $item->created_at->toDateTimeString();
                            unset($item->created_at);
                        });

        if ($income_list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'current'=> $page,
                'pages' =>  ceil($count / self::PAGE_LIMIT),
                'list'  =>  $income_list
            ]];
        }
    }
}
